package com.xyht.sca_s.student_manage_system.modules.stampManagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.modules.stampManagement.entity.SmsStampManagement;
import com.xyht.sca_s.student_manage_system.modules.stampManagement.entity.SmsStampUseLog;
import com.xyht.sca_s.student_manage_system.modules.stampManagement.entity.dto.SmsStampUseLogAddDto;
import com.xyht.sca_s.student_manage_system.modules.stampManagement.entity.vo.SmsStampInfoVo;
import com.xyht.sca_s.student_manage_system.modules.stampManagement.entity.vo.SmsStampUseLogVo;
import com.xyht.sca_s.student_manage_system.modules.stampManagement.entity.vo.SmsStampUserVo;
import com.xyht.sca_s.student_manage_system.modules.stampManagement.mapper.SmsStampManagementMapper;
import com.xyht.sca_s.student_manage_system.modules.stampManagement.mapper.SmsStampUseLogMapper;
import com.xyht.sca_s.student_manage_system.modules.stampManagement.service.SmsStampUseLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsApplication;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsApplicationMapper;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.common.util.TimeUtil.StringtoDate;

/**
 * <p>
 * 印章使用记录表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-05-31
 */
@Service
public class SmsStampUseLogServiceImpl extends ServiceImpl<SmsStampUseLogMapper, SmsStampUseLog> implements SmsStampUseLogService {

    @Resource
    private SmsStampUseLogMapper smsStampUseLogMapper;
    @Resource
    private SmsStampManagementMapper smsStampManagementMapper;
    @Resource
    private SmsApplicationMapper smsApplicationMapper;
    @Resource
    private SmsUserMapper smsUserMapper;
    @Resource
    private CacheUtil cacheUtil;

    @Override
    public ResponseResult addStampUseLog(String userId, String client, SmsStampUseLogAddDto smsStampUseLogAddDto) {
        if(isNullOrEmpty(smsStampUseLogAddDto.getAppType()) || isNullOrEmpty(smsStampUseLogAddDto.getExamineId())
        || isNullOrEmpty(smsStampUseLogAddDto.getStampId()) || isNullOrEmpty(smsStampUseLogAddDto.getDetailId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsStampUseLog smsStampUseLog = new SmsStampUseLog();
        BeanUtils.copyProperties(smsStampUseLogAddDto,smsStampUseLog);
        smsStampUseLog.setStampUseId(userId);
        smsStampUseLog.setClient(client);

        smsStampUseLogMapper.insert(smsStampUseLog);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getStampUseLogList(String beginTime, String endTime,String userName, Integer pageSize, Integer pageNum) {

        Page<SmsStampUseLog> page = new Page<>(pageNum,pageSize);
        List<SmsStampUseLogVo> stampUseLogList = new ArrayList<>();
        QueryWrapper<SmsStampUseLog> stampUseLogQueryWrapper = new QueryWrapper<>();
        if(!isNullOrEmpty(beginTime)){
            Date bgTime = StringtoDate(beginTime);
            stampUseLogQueryWrapper.lambda().ge(SmsStampUseLog::getCreateTime,bgTime);
        }
        if(!isNullOrEmpty(endTime)){
            Date edTime = StringtoDate(endTime);
            stampUseLogQueryWrapper.lambda().le(SmsStampUseLog::getCreateTime,edTime);
        }

        if(!isNullOrEmpty(userName)){
            List<String> userId = smsUserMapper.selectList(
                    new QueryWrapper<SmsUser>()
                            .lambda().
                            like(SmsUser::getRealName, userName))
                    .stream()
                    .map(SmsUser::getId)
                    .collect(Collectors.toList());
            stampUseLogQueryWrapper.lambda().in(SmsStampUseLog::getStampUseId,userId);
        }

        smsStampUseLogMapper.selectPage(page,stampUseLogQueryWrapper);
        stampUseLogList = page.getRecords()
                .stream()
                .map(stampUseLog -> {
                    SmsStampUseLogVo smsStampUseLogVo = new SmsStampUseLogVo();
                    BeanUtils.copyProperties(stampUseLog,smsStampUseLogVo);
                    //查询AppPath
                    SmsApplication smsApplication = smsApplicationMapper.selectOne(new QueryWrapper<SmsApplication>()
                            .lambda()
                            .eq(SmsApplication::getAppType, stampUseLog.getAppType())
                            .eq(SmsApplication::getAppClient, stampUseLog.getClient()));
                    if(smsApplication != null) {
                        smsStampUseLogVo.setAppPath(smsApplication.getAppPath());
                    }
                    //印章信息
                    SmsStampManagement smsStampManagement = smsStampManagementMapper.selectById(stampUseLog.getStampId());
                    SmsStampInfoVo smsStampInfoVo = new SmsStampInfoVo();
                    BeanUtils.copyProperties(smsStampManagement,smsStampInfoVo);
                    smsStampUseLogVo.setStampInfo(smsStampInfoVo);
                    //使用印章人的信息
                    SmsUser userInfo = cacheUtil.getUserInfo(stampUseLog.getStampUseId());
                    SmsStampUserVo smsStampUserVo = new SmsStampUserVo();
                    if(userInfo != null){
                        BeanUtils.copyProperties(userInfo,smsStampUserVo);
                    }
                    smsStampUseLogVo.setUserStampUserInfo(smsStampUserVo);
                    return smsStampUseLogVo;
                })
                .collect(Collectors.toList());
        int total = (int)page.getTotal();
        return CommonResult.success(stampUseLogList,total);
    }

    @Override
    public ResponseResult getStampUseLogDetail(String id) {
        if(isNullOrEmpty(id)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsStampUseLog smsStampUseLog = smsStampUseLogMapper.selectById(id);
        if(smsStampUseLog == null){
            return CommonResult.failed(CommonCodeEnum.STAMP_MANAGEMENT_NOT_EXIST);
        }

        SmsStampUseLogVo smsStampUseLogVo = new SmsStampUseLogVo();
        BeanUtils.copyProperties(smsStampUseLog,smsStampUseLogVo);
        SmsApplication smsApplication = smsApplicationMapper.selectOne(new QueryWrapper<SmsApplication>()
                .lambda()
                .eq(SmsApplication::getAppType, smsStampUseLog.getAppType())
                .eq(SmsApplication::getAppClient, smsStampUseLog.getClient()));
        if(smsApplication != null) {
            smsStampUseLogVo.setAppPath(smsApplication.getAppPath());
        }
        //印章信息
        SmsStampManagement smsStampManagement = smsStampManagementMapper.selectById(smsStampUseLog.getStampId());
        SmsStampInfoVo smsStampInfoVo = new SmsStampInfoVo();
        BeanUtils.copyProperties(smsStampManagement,smsStampInfoVo);
        smsStampUseLogVo.setStampInfo(smsStampInfoVo);
        //使用印章人的信息
        SmsUser userInfo = cacheUtil.getUserInfo(smsStampUseLog.getStampUseId());
        SmsStampUserVo smsStampUserVo = new SmsStampUserVo();
        if(userInfo != null){
            BeanUtils.copyProperties(userInfo,smsStampUserVo);
        }
        smsStampUseLogVo.setUserStampUserInfo(smsStampUserVo);
        return CommonResult.success(smsStampUseLogVo);
    }
}
